package com.glodon.zhuanli.front.web;

import com.glodon.framework.common.id.UUIDGenerator;
import com.glodon.framework.web.util.ResponseParam;
import com.glodon.zhuanli.common.ZhuanliBaseController;
import com.glodon.zhuanli.common.sms.SMSPasswordModify;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping(ZhuanliForgetController.BASE_PATH)
@Controller
public class ZhuanliForgetController extends ZhuanliBaseController {

    public static final String VCODE_SESSION = "forget_vcode" ;

    public static final String IDEN_SESSION = "forget_iden" ;

    public static final String BASE_PATH = "/front/forget" ;

    private UUIDGenerator forgetUserIden = new UUIDGenerator() ;

    @GetMapping
    public ModelAndView forget(){
        ModelAndView mv = new ModelAndView() ;
        mv.setViewName(BASE_PATH);
        return mv ;
    }

    @RequestMapping("vcode")
    @ResponseBody
    public ResponseParam forgetVCode(@RequestParam String mobile){
        if( !isMobile( mobile ) ){
            return ResponseParam.fail().addMessage("手机号格式不正确");
        }

        //发送短信息
        SMSPasswordModify smsPasswordModify = zlsmsService.sendMessage( new SMSPasswordModify(mobile)) ;
        if( !smsPasswordModify.isSuccess() ){
            return ResponseParam.fail().addMessage( smsPasswordModify.getMessage() ) ;
        }

        String vcode = smsPasswordModify.getVcode() ;

        log.info("发送短信验证码 {} 到手机号 {}" , vcode , mobile ) ;

        //注册的验证码
        getHttpServletRequest().getSession().setAttribute( VCODE_SESSION , vcode ) ;

        return ResponseParam.success() ;
    }

    /**
     * 重置密码提交
     * @param u 默认为手机号
     * @param vcode
     * @return
     */
    @PostMapping("validvcode")
    @ResponseBody
    public ResponseParam validVCode(@RequestParam String u , @RequestParam String vcode ) {
        //校验信息的完整性
        if( StringUtils.isBlank(u) || StringUtils.isBlank(vcode) ){
            return ResponseParam.fail().addMessage("请完善信息再提交") ;
        }

        //验证手机验证码
        Object sessionVCode = getHttpServletRequest().getSession().getAttribute( VCODE_SESSION ) ;
        if (sessionVCode==null || !StringUtils.equalsIgnoreCase(vcode, sessionVCode.toString())) {
            return ResponseParam.fail().addMessage("短信验证码不正确或已失效，请重新填写。");
        }

        boolean existUser = sysUserService.existMobile( u );
        if (!existUser) {
            return ResponseParam.fail().addMessage("该用户不存在，请注册") ;
        }

        //生成验证码的唯一标识
        String iden = forgetUserIden.getNextId() ;

        //注册的验证码
        getHttpServletRequest().getSession().setAttribute( VCODE_SESSION , iden ) ;

        return ResponseParam.success().addParam( "iden" , iden ) ;
    }

    /**
     * 重置密码提交
     * @param u
     * @param p
     * @param iden
     * @return
     */
    @PostMapping("reset")
    @ResponseBody
    public ResponseParam resetPassword( @RequestParam String u , @RequestParam String p , @RequestParam String iden ) {
        //校验信息的完整性
        if( StringUtils.isBlank(u) || StringUtils.isBlank(p) || StringUtils.isBlank(iden) ){
            return ResponseParam.fail().addMessage("请完善信息再提交") ;
        }

        Object sessionIden = getHttpServletRequest().getSession().getAttribute( VCODE_SESSION ) ;
        if (sessionIden==null || !StringUtils.equalsIgnoreCase(iden, sessionIden.toString())) {
            return ResponseParam.fail().addMessage("修改密码超时，请重新获取短信验证码");
        }

        //验证保存注册信息到公共表
        boolean existUser = sysUserService.existMobile(u);
        if (!existUser) {
            return ResponseParam.fail().addMessage("该用户不存在，请注册") ;
        } else {
            sysUserService.updatePassword( u , p ) ;
            getHttpServletRequest().getSession().removeAttribute( VCODE_SESSION ); ;
        }

        return ResponseParam.success();
    }


}
